package ru.agiledev.akb.client.panel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import ru.agiledev.akb.client.AKBDataService;
import ru.agiledev.akb.client.AKBDataServiceAsync;
import ru.agiledev.akb.client.utils.Util;
import ru.agiledev.akb.shared.QType;
import ru.agiledev.akb.shared.TransferObject;

/**
 * Created by a.kirillov
 * Date: 3/24/11 Time: 11:48 PM
 */
public class ExamplePanel extends VerticalPanel{

    HorizontalPanel iObjectsPanel;
    HorizontalPanel questionPanel;
    HorizontalPanel qTypePanel;
    VerticalPanel aspectsPanel;
    VerticalPanel examples;
    TextBox iObject1;
    TextBox iObject2;
    TextBox iObject3;
    TextArea questionField;
    ListBox qTypeListBox;
    TextArea aspectsField;
    TextArea examplesText;

    Integer[] pageIds = null;
    int selectedItemIndex = 0;

    private final AKBDataServiceAsync dataService = GWT.create(AKBDataService.class);

    public ExamplePanel(){
        setStyleName("tabPanel");
        if(pageIds==null){
            AsyncCallback<Integer[]> callback = new AsyncCallback<Integer[]>() {
            @Override
            public void onFailure(Throwable caught) {
                //TODO: error window
            }

                @Override
                public void onSuccess(Integer[] result) {
                    pageIds = result;
                }
        };

          dataService.getIds(callback);
        }

        questionPanel = new HorizontalPanel();
        getElement().getStyle().setPosition(Style.Position.RELATIVE);

        Label questionLabel = new Label("Вопрос:");
        questionPanel.add(questionLabel);

        questionField = new TextArea();
        questionField.setWidth("725px");
        questionPanel.add(questionField);
        questionPanel.setHeight("60px");

        add(questionPanel);


        iObjectsPanel = new HorizontalPanel();
        iObjectsPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

        Label objectsLabel = new Label("Объекты интереса");
        iObjectsPanel.add(objectsLabel);

        iObject1 = new TextBox();
        iObjectsPanel.add(iObject1);
        iObjectsPanel.add(iObject1);


        iObject2 = new TextBox();
        iObjectsPanel.add(iObject2);
        iObjectsPanel.add(iObject2);
        iObjectsPanel.setHeight("30px");

        iObject3 = new TextBox();
        iObject3.setVisible(false);
        iObjectsPanel.add(iObject3);
        iObjectsPanel.add(iObject3);
        iObjectsPanel.setHeight("30px");


        qTypePanel = new HorizontalPanel();
        qTypePanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

        final Label questionType = new Label("Тип вопроса");
        qTypePanel.add(questionType);
        qTypeListBox = new ListBox();

        String[] types = QType.getTypesAsArray();
        for(String type: types){
            qTypeListBox.addItem(type);
        }

        qTypePanel.add(qTypeListBox);
        qTypePanel.setHeight("30px");

        add(qTypePanel);
        add(iObjectsPanel);

        aspectsPanel = new VerticalPanel();

        Label aspects = new Label("Аспекты :");
        aspectsPanel.add(aspects);
        aspectsField = new TextArea();
        aspectsField.setWidth("774px");

        aspectsPanel.add(aspectsField);
        aspectsPanel.setHeight("80px");

        add(aspectsPanel);

        examples = new VerticalPanel();
        Label exampleLabel = new Label("Примеры :");
        examplesText = new TextArea();
        examplesText.setWidth("774px");
        examplesText.setHeight("150px");

        examples.add(exampleLabel);
        examples.add(examplesText);

        add(examples);

        HorizontalPanel buttonsPanel = new HorizontalPanel();

        ArrowClickHandler clickHandler = new ArrowClickHandler();

        Button forward = new Button("Следующий");
        forward.getElement().setId("forward");

        forward.addClickHandler(clickHandler);
        Button backward = new Button("Предыдущий");
        backward.getElement().setId("backward");
        backward.addClickHandler(clickHandler);

        buttonsPanel.add(backward);
        buttonsPanel.add(forward);

        add(buttonsPanel);
    }

    class ArrowClickHandler implements ClickHandler{
        @Override
        public void onClick(ClickEvent event) {
            Element e = ((Button)event.getSource()).getElement();

            if("forward".equals(e.getId())){
                if(selectedItemIndex == pageIds.length-1){
                    selectedItemIndex = 0;
                } else {
                    selectedItemIndex++;
                }

                getNewDTO(pageIds[selectedItemIndex]);
            }

            if("backward".equals(e.getId())){
                 if(selectedItemIndex == 0){
                    selectedItemIndex = pageIds.length-1;
                } else {
                    selectedItemIndex--;
                }

                getNewDTO(pageIds[selectedItemIndex]);
            }
        }
    }

    void getNewDTO(int id){
        AsyncCallback<TransferObject> DTOAsyncCallback = new AsyncCallback<TransferObject>() {
            @Override
            public void onFailure(Throwable caught) {
                //TODO: show error window or error message on top panel
            }

            @Override
            public void onSuccess(final TransferObject result) {
                if(result!=null){
                  updateVales(result);

                }
            }
        };

        dataService.getDataForQuestion(id, DTOAsyncCallback);
    }

    void updateVales(TransferObject dto){
        questionField.setValue(dto.getQuestion());
                    qTypeListBox.setItemSelected(Util.indexOfType(dto.getQuestionType()), true);

                    iObject1.setValue(dto.getiObjects()[0]);

                    iObject2.setValue(dto.getiObjects()[1]);
                    if(dto.getiObjects().length>2){
                        iObject3.setValue(dto.getiObjects()[2]);
                    }

                    aspectsField.setValue(Util.join(dto.getAspects(),", "));

                    examplesText.setValue(Util.join(dto.getExamples(), "; "));
    }

    public void setDefaultValues(){
        getNewDTO(pageIds[selectedItemIndex]);
    }
}




